/**
 * 
 */
package com.woniuxy.amusementparkos.permission.controller;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.fzw.Department;
import com.woniuxy.amusementparkos.entity.fzw.Menu;
import com.woniuxy.amusementparkos.entity.fzw.Role;
import com.woniuxy.amusementparkos.exception.fzw.MenuException;
import com.woniuxy.amusementparkos.exception.fzw.MenuInUseException;
import com.woniuxy.amusementparkos.exception.fzw.RoleException;
import com.woniuxy.amusementparkos.exception.fzw.RoleInUseException;
import com.woniuxy.amusementparkos.permission.Service.MenuService;
import com.woniuxy.amusementparkos.permission.Service.RoleService;
import com.woniuxy.amusementparkos.util.lzw.result.ResponseResult;

/**
 *
 * @author fzw
 *
 * 2020年4月26日下午7:39:46
 */
@Controller
public class MenuManagementController {
	//自动注入
	@Autowired
	MenuService menuService;
	
	/**
	 * 获取一页的菜单数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/menuMg/getAllMenu.do")
	@ResponseBody
	public TableDataDto<Menu> getAllRole(@Param("page")Integer page,@Param("limit")Integer limit){
		TableDataDto<Menu> res = new TableDataDto<Menu>();
		try {
			//查询一页的角色信息
			List<Menu> menus=menuService.selectAllMenuByPageNumber(page,limit);
			Long count=menuService.selectCount();
			res.setCount(count);
			res.setData(menus);
			res.setCode(200);
			res.setMsg("获取菜单数据成功");
			return res;
		} catch (RoleException e) {
			res.setMsg("菜单处理异常，请联系管理员！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/menuMg/insertMenu")
	@ResponseBody
	public ResponseResult<Menu> insertMenu(Menu menu){
		ResponseResult<Menu> res = new ResponseResult<Menu>(404,"服务器错误",null);
		try {
			//插入角色信息
			menuService.insertMenu(menu);
			res.setCode(200);
			res.setMsg("添加成功！");
			return res;
		} catch (RoleException e) {
			res.setMsg("菜单添加异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/menuMg/updateOne")
	@ResponseBody
	public ResponseResult<Menu> updateRole(Menu menu){
		ResponseResult<Menu> res = new ResponseResult<Menu>(404,"服务器错误",null);
		try {
			//插入角色信息
			menuService.updateMenu(menu);
			res.setCode(200);
			res.setMsg("添加成功！");
			return res;
		} catch (RoleException e) {
			res.setMsg("菜单修改异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/menuMg/deleteMenus")
	@ResponseBody
	public ResponseResult<Menu> deleteMenus(int[] menu_ids){
		ResponseResult<Menu> res = new ResponseResult<Menu>(404,"服务器错误",null);
		try {
			//删除菜单信息前先检验该角色是否正被用户使用，即被从表关联
			menuService.selectMenuByMenuIds(menu_ids);
			//删除角色信息
			menuService.deleteMenus(menu_ids);
			res.setCode(200);
			res.setMsg("删除成功！");
			return res;
		}catch (MenuInUseException e) {
			res.setMsg(e.getMessage());
			res.setCode(4);
			return res;
		} 
		catch (MenuException e) {
			res.setMsg("菜单删除异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	
	@RequestMapping("/menu/selectAllMenu")
	@ResponseBody
	public ResponseResult<List<Menu>> selectAllMenu(){
		//初始值
		ResponseResult<List<Menu>> result = new ResponseResult<List<Menu>>(404,"服务器错误！",null);
		
		try {
				//查询所有菜单
			List<Menu> menus=menuService.selectAllMenu();
				//没有异常则设置
			result.setData(menus);
		} catch (RuntimeException e) {
			result.setCode(4);
			result.setMsg("查询失败！请联系工作人员！");
			return result;
		}catch (Exception e) {
			result.setCode(4);
			result.setMsg("系统错误！");
			return result;
		}
		result.setCode(200);
		result.setMsg("查询所有菜单成功！");
		return result;
	}
}
